/*
package com.ywh.auth.controller;

import org.keycloak.adapters.springsecurity.client.KeycloakRestTemplate;
import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.idm.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
public class IndexController {

    @Autowired
    private KeycloakRestTemplate keycloakRestTemplate;

    @Autowired
    private Keycloak keycloak;

    @Value("${keycloak.realm}")
    private String realm;

    @RequestMapping(value = "/secret", method = RequestMethod.GET)
    public String index() {
        RealmResource realm = keycloak.realm(this.realm);
        UsersResource users = realm.users();
        List<UserRepresentation> list = users.list();

        TokenManager tokenManager = keycloak.tokenManager();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        AccessTokenResponse accessToken = tokenManager.getAccessToken();
        String userId = String.valueOf(authentication.getPrincipal());
        ClientResource clientResource = realm.clients().get(userId);
        List<ClientRepresentation> byClientId = realm.clients().findByClientId("spring-client");
        ClientRepresentation clientRepresentation = byClientId.get(0);
        List<String> defaultClientScopes = clientRepresentation.getDefaultClientScopes();
        String clientId = clientRepresentation.getClientId();

        ClientScopesResource clientScopesResource = realm.clientScopes();
        List<ClientScopeRepresentation> all = clientScopesResource.findAll();
        //重新生成secret
//        CredentialRepresentation credentialRepresentation = clientResource.generateNewSecret();

//        UserResource userResource = users.get(userId);
//        Map<String, Object> impersonate = userResource.impersonate();
//        List<Map<String, Object>> consents = userResource.getConsents();
//        List<UserSessionRepresentation> userSessions = userResource.getUserSessions();
//        UserRepresentation user = new UserRepresentation();
//        user.setUsername("johndoe1231");
//        user.setEnabled(true);
//
//        RealmRepresentation realm = new RealmRepresentation();
//        realm.setRealm("dddd");

//        RoleRepresentation roleRepresentation = new RoleRepresentation();
//        roleRepresentation.setName("dddd");
//        roleRepresentation.setClientRole(true);
//        roleRepresentation.setId("909090909");
//        management.realm("spring-boot-quickstart").roles().create(roleRepresentation);
//        management.realm("spring-boot-quickstart").users().create(user);
//        List forObject = keycloakRestTemplate.getForObject(URI.create("http://127.0.0.1:8080/auth" + "/admin/realms/tcheck/users"), List.class);
        return "index";
    }

    @RequestMapping("/createUser")
    public String createUser() {
        RealmRepresentation realmRepresentation = new RealmRepresentation();
        realmRepresentation.setRealm("newRealm");

        //构建client
        List<ClientRepresentation> setClients = new ArrayList<>();
        ClientRepresentation add = new ClientRepresentation();
        ArrayList<String> objects = new ArrayList<>();
        objects.add("http://127.0.0.1:8010/*");
        add.setRedirectUris(objects);
        add.setStandardFlowEnabled(true);
        add.setDirectAccessGrantsEnabled(true);
        add.setServiceAccountsEnabled(true);
        add.setClientId("newclient");
        setClients.add(add);
        realmRepresentation.setClients(setClients);

        //构建user
        List<UserRepresentation> setUsers = new ArrayList<>();
        UserRepresentation userRepresentation = new UserRepresentation();
        userRepresentation.setEnabled(true);
        userRepresentation.setEmail("jfldsfsf@qq.com");
        userRepresentation.setUsername("newuser");
        List<String> userRoleList = new ArrayList<>();
        userRoleList.add("newone");
        userRepresentation.setRealmRoles(userRoleList);
        setUsers.add(userRepresentation);
        realmRepresentation.setUsers(setUsers);

        //构建role
        RolesRepresentation setRoles = new RolesRepresentation();
        List<RoleRepresentation> roleRepresentationList = new ArrayList<>();
        RoleRepresentation roleRepresentation = new RoleRepresentation();
        roleRepresentation.setName("newone");
        roleRepresentation.setClientRole(false);
        roleRepresentationList.add(roleRepresentation);
        setRoles.setRealm(roleRepresentationList);
        realmRepresentation.setRoles(setRoles);

        keycloak.realms().create(realmRepresentation);
        return "createUser";
    }

    @RequestMapping("/queryRealm")
    public String queryRealm() {
//        RealmResource newRealm = keycloak.realm("newRealm");
//        ClientScopesResource clientScopesResource = newRealm.clientScopes();
//        List<ClientScopeRepresentation> all = clientScopesResource.findAll();
//        ClientRepresentation create = new ClientRepresentation();
//        create.setClientId("spring-realm");
//        newRealm.clients().create(create);
        return "success";
    }

    @RequestMapping("/updateClient")
    public String updateClient() {
      */
/*  RealmResource newRealm = keycloak.realm("newRealm");
        List<ClientRepresentation> byClientId = newRealm.clients().findByClientId("newclient");
        ClientRepresentation clientRepresentation = byClientId.get(0);
        clientRepresentation.setAuthorizationServicesEnabled(false);

        newRealm.clients().get(clientRepresentation.getId()).update(clientRepresentation);*//*


*/
/*        RealmRepresentation realmRepresentation = newRealm.toRepresentation();
        realmRepresentation.setClients(byClientId);
        realmRepresentation.setVerifyEmail(true);

        newRealm.update(realmRepresentation);*//*

        return "success";
    }

}
*/
